<?php

namespace app\stock\controller\Manage;

use think\Controller;

class Source extends Controller
{
    protected $SourceModel = null;

    public function __construct()
    {
        parent::__construct();

        $this->SourceModel = model('Business.Source');
    }

    public function index()
    {
        $data = $this->SourceModel->order('id DESC')->select();

        if (!$data) {
            $this->error('暂无数据');
        }

        $this->success('查询成功', null, $data);
    }

    public function add()
    {
        $name = $this->request->param('name', '', 'trim');

        $result = $this->SourceModel->validate('common/Business/Source')->save(['name' => $name]);

        if ($result === FALSE) {
            $this->error($this->SourceModel->getError());
        } else {
            $this->success('新增成功');
        }
    }

    public function del()
    {
        $id = $this->request->param('id', 0, 'trim');

        $Source = $this->SourceModel->find($id);

        if (!$Source) {
            $this->error('该客户来源不存在');
        }

        $result = $Source->delete();

        if ($result === FALSE) {
            $this->error('删除失败');
        } else {
            $this->success('删除成功');
        }
    }

    public function info()
    {
        $id = $this->request->param('id', 0, 'trim');

        $Source = $this->SourceModel->find($id);

        if (!$Source) {
            $this->error('该客户来源不存在');
        }

        $this->success('返回数据成功', null, $Source);
    }

    public function edit()
    {
        $id = $this->request->param('id', 0, 'trim');
        $name = $this->request->param('name', '', 'trim');

        $Source = $this->SourceModel->find($id);

        if (!$Source) {
            $this->error('该客户来源不存在');
        }

        $Source->name = $name;

        $result = $Source->save();

        if ($result === FALSE) {
            $this->error('编辑失败');
        } else {
            $this->success('编辑成功');
        }
    }
}
